#include "subcurve.h"

SubCurve::SubCurve(QWidget *parent) : QCustomPlot(parent)
{

}
SubCurve::~SubCurve()
{

}

void SubCurve::fun_intUi1()
{
    QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
    dateTicker->setDateTimeFormat("hh:mm:ss");
    dateTicker->setDateTimeSpec(Qt::LocalTime);
    this->xAxis->setTicker(dateTicker);
    //四边安上坐标轴
    //this->axisRect()->setupFullAxesBox();
    // 使上下轴、左右轴范围同步
    connect(this->xAxis, SIGNAL(rangeChanged(QCPRange)), this->xAxis2, SLOT(setRange(QCPRange)));
    connect(this->yAxis, SIGNAL(rangeChanged(QCPRange)), this->yAxis2, SLOT(setRange(QCPRange)));
    //设置缩放方式iRangeDrag可拖动 iRangeZoom可缩放 iSelectPlottables选中曲线
    this->setInteractions(QCP::iRangeZoom );
    //设置主背景图
    this->setBackground(QColor(44,44,45,255));
//    this->yAxis->ticker()->setTickCount(3);//主刻度个数设置
    this->xAxis->ticker()->setTickCount(3);//主刻度个数设置
    //this->axisRect()->setBackground(QColor(0x00,0xff,0x00,0x00));//这种网格背景黑色，透明颜色
    //设置XY坐标的网格样式
    this->xAxis->grid()->setPen(QPen(QColor(78, 78, 78), 1, Qt::PenStyle::SolidLine));//网格白实线
//    this->xAxis->grid()->setSubGridPen(QPen(QColor(220, 200, 50), 1, Qt::SolidLine));//网格黄色实线
//    this->xAxis->grid()->setSubGridVisible(true);//显示x轴子网格线
//    this->xAxis->grid()->setZeroLinePen(QPen(Qt::white));//x轴0线颜色白色
    this->yAxis->grid()->setPen(QPen(QColor(78, 78, 78), 1, Qt::PenStyle::DashLine));//网格白色虚线
//    this->yAxis->grid()->setSubGridPen(QPen(QColor(50, 50, 50), 1, Qt::SolidLine));//网格浅黑色点线
//    this->yAxis->grid()->setSubGridVisible(true);//显示Y轴子网格线
//    this->yAxis->grid()->setZeroLinePen(QPen(Qt::white));//y轴0线颜色白色
    //设置轴线颜色
    this->xAxis->setBasePen(QPen(QColor(78,78,78),1));
    this->yAxis->setBasePen(QPen(QColor(78,78,78),1));
    //刻度线长度和颜色

    this->xAxis->setTickPen(QPen(QColor(78,78,78),1));
    this->yAxis->setTickPen(QPen(QColor(78,78,78),1));
//    this->xAxis->setTickLengthIn(0);//主刻度向内延伸15
//    this->xAxis->setTickLengthOut(0);//主刻度向外延伸0
//    pen.setColor(Qt::blue);//子刻度蓝色
//    this->xAxis->setSubTickPen(pen);
//    this->xAxis->setSubTickLengthIn(0);//子刻度向内延伸10
//    this->xAxis->setSubTickLengthOut(0);//子刻度向外延伸0

    this->xAxis->setSubTicks(false);//隐藏子刻度
    this->yAxis->setSubTicks(false);//隐藏子刻度
    //刻度字体设置
    this->xAxis->setTickLabelFont(QFont("Microsoft YaHei", 12, 32));
    this->xAxis->setTickLabelColor(QColor(Qt::white));
    this->yAxis->setTickLabelFont(QFont("Microsoft YaHei", 10, 30));
    this->yAxis->setTickLabelColor(QColor(Qt::white));
    //标题字体设置
    this->xAxis->setLabelFont(QFont("Microsoft YaHei", 14, 30));
    this->xAxis->setLabelColor(QColor(Qt::white));
    this->yAxis->setLabelFont(QFont("Microsoft YaHei", 14, 30));
    this->yAxis->setLabelColor(QColor(Qt::white));
}

void SubCurve::fun_intUi2()
{
    //设置刻度显示类型
//    QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
//    dateTicker->setDateTimeFormat("MM-dd hh:mm:ss");
//    dateTicker->setDateTimeSpec(Qt::LocalTime);
//    this->xAxis->setTicker(dateTicker);

//     QSharedPointer<QCPAxisTickerText> Ticker_Y(new QCPAxisTickerText);
//     Ticker_Y->setTickCount(5);
//     Ticker_Y->setSubTickCount(3);
//     this->yAxis2->setTicker(Ticker_Y);
    //四边安上坐标轴
//    this->axisRect()->setupFullAxesBox();
    // 使上下轴、左右轴范围同步
    connect(this->xAxis, SIGNAL(rangeChanged(QCPRange)), this->xAxis2, SLOT(setRange(QCPRange)));
    connect(this->yAxis, SIGNAL(rangeChanged(QCPRange)), this->yAxis2, SLOT(setRange(QCPRange)));
    connect(this->yAxis2, SIGNAL(rangeChanged(QCPRange)), this->yAxis, SLOT(setRange(QCPRange)));
    //设置缩放方式iRangeDrag可拖动 iRangeZoom可缩放 iSelectPlottables选中曲线
    this->setInteractions(QCP::iRangeZoom|QCP::iRangeDrag);
    //设置主背景图
    //this->setBackground(QPixmap(":/images/background_W.png",nullptr,Qt::OrderedDither));
    this->setBackground(QColor(0,0,0,255));
    this->axisRect()->setBackground(QColor(0x00,0xff,0x00,0x00));//这种网格背景黑色，透明颜色

    //设置XY坐标的网格样式
    this->yAxis->ticker()->setTickCount(3);//主刻度个数设置
    this->xAxis->ticker()->setTickCount(3);//主刻度个数设置
    this->xAxis->grid()->setPen(QPen(QColor(0, 57, 63), 1, Qt::SolidLine));//网格白色虚线
    this->xAxis->grid()->setSubGridPen(QPen(QColor(0, 57, 63), 1, Qt::SolidLine));//网格黄色色点线
    this->xAxis->grid()->setSubGridVisible(false);//显示x轴子网格线
    this->xAxis->grid()->setZeroLinePen(QPen(QColor(0, 57, 63)));//x轴0线颜色白

    this->yAxis->grid()->setPen(QPen(QColor(0, 57, 63), 1, Qt::SolidLine));//网格白色虚线
    this->yAxis->grid()->setSubGridPen(QPen(QColor(0, 57, 63), 1, Qt::SolidLine));//网格浅黑色点线
    this->yAxis->grid()->setSubGridVisible(true);//显示Y轴子网格线
    this->yAxis->grid()->setZeroLinePen(QPen(QColor(0, 57, 63)));//y轴0线颜色白色
    //设置轴线颜色
    QColor AxisColor(QColor(0, 57, 63));
    this->xAxis->setBasePen(QPen(AxisColor));
    this->yAxis->setBasePen(QPen(AxisColor));
    this->yAxis2->setBasePen(QPen(AxisColor));
    //刻度线长度和颜色
    QPen pen;
    pen.setColor(AxisColor);//主刻度红色
//    pen.setWidth(2);//线宽2
    this->xAxis->setTickPen(pen);
//    this->xAxis->setTickLengthIn(15);//主刻度向内延伸15
//    this->xAxis->setTickLengthOut(0);//主刻度向外延伸0
    pen.setColor(AxisColor);//子刻度蓝色
    this->xAxis->setSubTickPen(pen);
//    this->xAxis->setSubTickLengthIn(10);//子刻度向内延伸10
//    this->xAxis->setSubTickLengthOut(0);//子刻度向外延伸0

    this->yAxis->setTickPen(pen);
    this->yAxis2->setTickPen(pen);
    this->yAxis->setSubTickPen(pen);
    this->yAxis2->setSubTickPen(pen);
    //刻度字体设置
    this->xAxis->setTickLabelFont(QFont("Microsoft YaHei", 12, 32));
    this->xAxis->setTickLabelColor(QColor(Qt::white));
    this->yAxis->setTickLabelFont(QFont("Microsoft YaHei", 10, 30));
    this->yAxis->setTickLabelColor(QColor(Qt::white));
    this->yAxis2->setTickLabelColor(QColor(Qt::white));
    //刻度数量
    //this->xAxis->ticker()->setTickOrigin(3.8);
    //this->xAxis->ticker()->setTickStepStrategy(QCPAxisTicker::tssReadability);//可读性优于设置
    //刻度值格式
    //this->xAxis->setNumberFormat("gbc");//g灵活的格式,b漂亮的指数形式，c乘号改成×
    //this->xAxis->setNumberPrecision(1);//精度1

    //改变起始原点
    //this->xAxis->setRange(-15,15);
    //this->xAxis->ticker()->setTickOrigin(1);//改变刻度原点为1

    //线性结尾装饰
    this->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);//x轴终点箭头图案
    //    this->xAxis->setLowerEnding(QCPLineEnding::esDisc);//x轴起点圆点图案
    this->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);//Y轴终点箭头图案

    //轴位置偏移量
    //this->xAxis->setPadding(50);//填充50的空间
    //this->xAxis->setOffset(50);//偏移50
    //this->yAxis->setOffset(-50);//偏移50
    //this->yAxis->setPadding(-5);//填充50的空间
    //标题字体设置
    this->xAxis->setLabelFont(QFont("Microsoft YaHei", 14, 30));
    this->xAxis->setLabelColor(QColor(Qt::white));
    //    this->xAxis->setLabel("X");
    this->yAxis->setLabelFont(QFont("Microsoft YaHei", 14, 30));
    this->yAxis->setLabelColor(QColor(Qt::white));
    //    this->yAxis->setLabel("Y");
}
void SubCurve::fun_addGraph(int index)
{
    QColor color(255,255,255);
    switch (index)
    {
    case 0:
        color=QColor(255,0,0);
        break;
    case 1:
        color=QColor(249,143,0);
        break;
    case 2:
        color=QColor(0,122,255);
        break;
    case 3:
        color=QColor(90,133,131);
        break;
    case 4:
        color=QColor(0,171,0);
        break;
    case 5:
        color=QColor(120,21,162);
        break;
    case 6:
        color=QColor(255,0,128);
        break;
    case 7:
        color=QColor(0,181,205);
        break;
    default:
        break;
    }
    QPen pen;
    pen.setColor(color);
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(2);
    this->addGraph();
    this->graph(index)->setPen(pen);
    this->graph(index)->setLineStyle(QCPGraph::lsLine);
}

void SubCurve::slots_Xzoom(int val)
{
    double lower1=this->xAxis->range().lower;
    double upper1=this->xAxis->range().upper;

    double multiple=1.3;
    double zoom=1.0-(1.0/multiple);
    double midpoint1=(lower1+upper1)/2.0;//坐标中点
    double diffVal1=(upper1-midpoint1)*zoom;

    double lower2=lower1+diffVal1;
    double upper2=upper1-diffVal1;
    double midpoint2=(lower2+upper2)/2.0;//坐标中点
    double diffVal2=(upper2-midpoint2)*zoom;
    double coefficient=diffVal1/diffVal2;

   // qDebug()<<"diffVal"<<diffVal1<<diffVal2<<coefficient;
    switch (val)
    {
    case 0://缩小
        this->xAxis->setRange(lower1-diffVal1,upper1+diffVal1);
        break;
    case 1://放大
        this->xAxis->setRange(lower1+diffVal1,upper1-diffVal1);
        break;
    default:
        break;
    }
    this->replot(QCustomPlot::rpQueuedReplot);
}

void SubCurve::slots_Yzoom(int val)
{
    double lower1=this->yAxis->range().lower;
    double upper1=this->yAxis->range().upper;

    double multiple=1.3f;
    double zoom=1.0-(1.0/multiple);
    double midpoint1=(lower1+upper1)/2.0;//坐标中点
    double diffVal1=(upper1-midpoint1)*zoom;

    double lower2=lower1+diffVal1;
    double upper2=upper1-diffVal1;
    double midpoint2=(lower2+upper2)/2.0;//坐标中点
    double diffVal2=(upper2-midpoint2)*zoom;
    double coefficient=diffVal1/diffVal2;

    switch (val)
    {
    case 0://缩小
        diffVal1=diffVal1*coefficient;
        this->yAxis->setRange(lower1-diffVal1,upper1+diffVal1);
        break;
    case 1://放大
        this->yAxis->setRange(lower1+diffVal1,upper1-diffVal1);
        break;
    default:
        break;
    }
    this->replot(QCustomPlot::rpQueuedReplot);
}

void SubCurve::slots_XYoffSet(int val)
{
    double xlower=this->xAxis->range().lower;
    double xupper=this->xAxis->range().upper;
    double ylower=this->yAxis->range().lower;
    double yupper=this->yAxis->range().upper;
    double diffVal_X=(xupper-xlower)*(5.f/100.f);
    double diffVal_Y=(yupper-ylower)*(5.f/100.f);

    switch (val)
    {
    case 0:
        this->xAxis->moveRange(-1.0*diffVal_X);
        break;
    case 1:
        this->yAxis->moveRange(diffVal_Y);
        break;
    case 2:
        this->xAxis->moveRange(diffVal_X);
        break;
    case 3:
        this->yAxis->moveRange(-1.0*diffVal_Y);
        break;
    default:
        break;
    }
    this->replot(QCustomPlot::rpQueuedReplot);
}
